package utils
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author FireKnight
	 */
	public final class FitSpriteSize
	{
		
		public function FitSpriteSize()
		{
		
		}
		
		public static function fitOut(source:*, cur_width:Number, cur_height:Number,importLength:String="width"):*
		{
			var rat:Number = source.width / source.height;
			if (source.width >= source.height)
			{
				if (source.width > cur_width)
				{
					source.width = cur_width;
					source.height = cur_width / rat;
				}
				
				if (importLength == "height")
				{
					
					if (source.height < cur_height)
					{
						source.height = cur_height;
						source.width = cur_height * rat;
					}
				}
			}
			else
			{
				if (source.height > cur_height)
				{
					source.height = cur_height;
					source.width = cur_height * rat;
				}
				if (importLength == "width")
				{
					if (source.width < cur_width)
					{
						source.width = cur_width;
						source.height = cur_width / rat;
					}
				}
			}
			return source;
		}
		
		public static function maxFit(source:*, cur_width:Number, cur_height:Number):*
		{
			//如果比目标小就直接用下面的方法
			if (cur_width < source.width) return fitImage(source, cur_width, cur_height);
			var maxRat:Number;
			if (source.width >= source.height)
			{
				maxRat = cur_width / source.width;//获取宽度最大倍数
				for (var i:Number = maxRat; i > 1; i -= 0.01)
				{
					if ((source.height *=i) <= cur_height)
					{
						maxRat = i;
						break;
					}
				}
			}
			else
			{
				maxRat = cur_height / source.height;//获取宽度最大倍数
				for (var o:Number = maxRat; o > 1; o -= 0.01)
				{
					if ((source.height *= o)<= cur_height)
					{
						maxRat = o;
						break;
					}
				}
			}
			source.width *= maxRat;
			source.height *= maxRat;
			return source;
		}
		
		public static function fitImage(img_loader:*,_maxWidth:Number,_maxHeight:Number):*
		{
			var rat:Number = img_loader.width / img_loader.height;
			if (img_loader.width >= img_loader.height)
			{
				if (img_loader.width > _maxWidth)
				{
					img_loader.width = _maxWidth;
					img_loader.height = _maxWidth / rat;
				}
				if (img_loader.height > _maxHeight)
				{
					img_loader.height = _maxHeight;
					img_loader.width = _maxHeight * rat;
				}
			}
			else
			{
				if (img_loader.height > _maxHeight)
				{
					img_loader.height = _maxHeight;
					img_loader.width = _maxHeight * rat;
				}
				if (img_loader.width > _maxWidth)
				{
					img_loader.width = _maxWidth;
					img_loader.height = _maxWidth / rat;
				}
			}
			return img_loader;
		}
		
		public static function fitMovieClipMax(img_loader:MovieClip,_maxWidth:Number,_maxHeight:Number):*
		{
			var rat:Number = img_loader.width / img_loader.height;
			if (img_loader.width >= img_loader.height)
			{
				img_loader.width = _maxWidth;
				img_loader.height = _maxWidth / rat;
			}
			else
			{
				img_loader.height = _maxHeight;
				img_loader.width = _maxHeight * rat;
			}
			if(img_loader.height>_maxHeight)
			{
				img_loader.height = _maxHeight;
				img_loader.width = _maxHeight * rat;
			}
			
			return img_loader;
		}
	
	}

}